package cn.agiledata.bank.linkbank.impl.cmbc;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;

import cn.agiledata.bank.common.util.DateUtil;
import cn.agiledata.bank.linkbank.exception.LinkBankException;
import cn.agiledata.bank.linkbank.impl.AbstractBankPayResponse;
import cn.agiledata.bank.linkbank.logic.DigitalSignValidatable;
import cn.agiledata.bank.linkbank.logic.OrderIdTransformer;

/**
 * 中国民生银行网上银行支付响应
 * 
 * @author jessik
 * 
 */
public final class CmbcNetPayResponse extends AbstractBankPayResponse
		implements DigitalSignValidatable {

	private static final String[] RESPONSE_KEYS = { CmbcNetConstant.BIll_NO,
			CmbcNetConstant.CORP_ID, CmbcNetConstant.TX_AMT,
			CmbcNetConstant.TX_DATE, CmbcNetConstant.TX_TIME,
			CmbcNetConstant.BILL_STATUS, CmbcNetConstant.BILL_REMARK_1,
			CmbcNetConstant.BILL_REMARK_2 };

	private String keyFilePath;

	private OrderIdTransformer orderIdTransformer;

	protected AbstractBankPayResponse getCopiedInstance() {
		CmbcNetPayResponse response = new CmbcNetPayResponse();
		response.keyFilePath = this.keyFilePath;
		response.orderIdTransformer = this.orderIdTransformer;
		return response;
	}

	public void setKeyFilePath(String keyFilePath) {
		this.keyFilePath = keyFilePath;
	}

	public boolean isAmountValidatable() {
		return true;
	}

	private OrderIdTransformer getOrderIdTransformer() {// 订单生成器
		if (this.orderIdTransformer == null) {
			this.orderIdTransformer = new CmbcOrderIdTransformer(
					getMerchantId());
		}
		return this.orderIdTransformer;
	}

	public String getOrderId() {// 定单号（商户号（5位）＋商户自定义序号（15位）
		
		String temp =this.getOrderIdTransformer().orderId2TransId(
				this.getMessage(CmbcNetConstant.BIll_NO));
		//String  t =ISOUtil.hexString(temp.getBytes());
		return  temp;
	}

	public String getMerchantId() { // 商户号（5位，银行事先分配）
		return this.getMessage(CmbcNetConstant.CORP_ID);
	}

	public BigDecimal getAmount() {// 交易金额
		try {
			return new BigDecimal(this.getMessage(CmbcNetConstant.TX_AMT));
		} catch (Exception e) {
			return null;
		}
	}

	public Date getDate() {// 交易日期和时间
		String dateValue = getMessage(CmbcNetConstant.TX_DATE);
		String timeValue = getMessage(CmbcNetConstant.TX_TIME);
		return DateUtil.string2Date(dateValue + " " + timeValue,
				CmbcNetConstant.TX_DATE_FORMAT + " "
						+ CmbcNetConstant.TX_TIME_FORMAT);
	}

	public String getSuccessReturnCode() {
		return CmbcNetConstant.BILL_SUCCESS;
	}

	public String getDigitalSign() {
		return this.getMessage(CmbcNetConstant.PAY_RESULT);
	}

	public boolean validateDigitalSign(String cryptoKey)
			throws LinkBankException {
		String[] keys = RESPONSE_KEYS;
		Map values = CmbcUtils.parseDigitalSign(getDigitalSign(), keys,
				cryptoKey, this.keyFilePath);
		this.setMessages(values);
		return true;
	}

	public String getCryptoKeyDescription() {
		return null;
	}

	public boolean validateCryptoKey(String cryptoKey) {
		return true;
	}

	public String getReturnCode() {// 订单状态
		return this.getMessage(CmbcNetConstant.BILL_STATUS);
	}

}
